
import threading
import  time

class DriveImpl:

    workers = []
    threadWorker = []


    def __init__(self,ctsToken):
        self.ctsToken = ctsToken

    #停止任务
    def stop(self):
        self.ctsToken.clear()

    def add(self,worker):
        self.workers.append(worker)

    def adds(self,workers):
        for worker in workers:
            self.workers.append(worker)

    def execute(self):
        print('驱动实例开始中了')
        if len(self.workers) == 0:
            raise Exception("至少需要一个工作者")

        for worker in self.workers:
            try:
                # 创建线程

                thread = threading.Thread(target=worker.handler, args=("World",self.ctsToken,))
                # 启动线程
                thread.start()

                self.threadWorker.append(thread)

            except Exception as ex:
                print('发生错误')
                print(ex)

        while True:
            try:
                isAlive = False

                for thread in self.threadWorker:
                    if thread.isAlive():
                        isAlive = True
                        break
                if isAlive is False:
                    break

            except  Exception as ex:
                print('d')
